package math2.delaunay;

import static java.lang.Math.abs;
import static java.lang.Math.sqrt;

public class Point {

	private static final double EPS = 1e-10;

	private double x, y;

	public Point(double x, double y) {
		this.x = x;
		this.y = y;
	}

	public Point add(double a, Point p) {
		return new Point(this.x + a * p.x, this.y + a * p.y);
	}

	public boolean areColinear(Point p1, Point p2) {
		Point d1 = p1.add(-1, this);
		Point d2 = p2.add(-1, this);
		return abs(d1.x * d2.y - d2.x * d1.y) <= EPS;
	}

	public double distanceTo(Point p) {
		return Math.sqrt((this.x - p.x) * (this.x - p.x) + (this.y - p.y) * (this.y - p.y));
	}

	public double getX() {
		return this.x;
	}

	public double getY() {
		return this.y;
	}

	public double norm2() {
		return sqrt(this.scalarProduct(this));
	}

	public double scalarProduct(Point p) {
		return this.x * p.x + this.y * p.y;
	}

	public double[] toArray() {
		return new double[] { this.x, this.y, 0 };
	}

	public double[] getCoordinate() {
		return new double[] { this.x, this.y };
	}
}
